Ch3 Common programming concepts **
2026-01-30 ใน The rust programming language 3rd edition(online document)3.1 Variable and Mutability
- variables
- valid only in their scope or before redefined by shadowing
const(constant)- Uppercase only
- must define the type of variable
- can never be changed, immutable by itself.
- constant expression: constant value will be calculate firstly on compile.
- shadowing
- use
letto define the variable again to override the old value and this can also overide type of variable. - the new set variable can be immutable again, though it was set to
mutbefore - The new variable also only work within its scope, and after the scope is end, if there is the old defined variable, the value will return to be the old value
- use
How to apply this
mutcan't change type, but the value bound to variable, and will retain the value from sub {scope} to the higher {scope}- shadowing can change the type of the varible, and the new value will retain only within the sub {scope}
- though the variable is
constyou can define as mathematical calculation, it will be calculated first and changed to constant expression.
3.2 Data Types
- scalar types = unscalable
-
integer
- usize, isize - depend on system architecture
- binanary calculation
- interger overflow : when the value exceed the maximum capacity of variable types it will return to the initial value like the rolling number in analog mile gauge.
- eg.
- 255:u8 = 11111111 in binary
- 256:u8 = 100000000 in binary but the memory size has only 8 bit so the first bit will be cut off which result in 00000000 which is = 0
- eg.
Two's complement**- in computer the minus number can't be easily calculated in binary, because subtracting is an another algorihm ie., if the minus sign is determined as the first bit and the following bit is the same as positive number it will be like this
- 1:i4=0001, -1:i4=1001 which is not efficient, if it is subtraction like 1-1 = 0001:binary-1001:binary, and also, if 0:i4=0000 what about -0:i4=1000 which is quite problematic for cpu circuit design
- so engineer create the new type, instead. They determined the roll back number that the first bit is 1 as negative number(but roll back from the maximum binary number) eg. -1=1111, -2=1110
- this will make subtraction very simple by just simply directly add it together like 0001(1:i4) + 1111(-1:i4) = 10000 which the exceed bit will be cut off and the result will be 0000 which is 0:i4. And there will be only 1 zero which is 0:i4=0000
- It's like taking advantage of the nature of binary number that has only 1,0.
- To calculate the negative number fast, you can just invert all the positive bits then plus 1 to the last bit .(this also take the advantage from nature of binary number) eg.
- 1:i8=00000001 then to create -1,
- invert all positive bits: 01111110
- add 1 to the first and the last bit : -1:i8=11111111 Try the subtraction 1-1:i8 = 00000001+11111111 = 100000000, Then the first exceeded bit will be cut off and the result will be 00000000 which is 0:i8
- 1:i8=00000001 then to create -1,
- in computer the minus number can't be easily calculated in binary, because subtracting is an another algorihm ie., if the minus sign is determined as the first bit and the following bit is the same as positive number it will be like this
- interger overflow : when the value exceed the maximum capacity of variable types it will return to the initial value like the rolling number in analog mile gauge.
-
float
- f32, f64 char compound type
-
tuple:(i32,f64,char)=(1,1.2,'c');
- any subtuple can be a different type.
- use tuple.index to access the member.
-
array
- all arrays are the same type
- can't increase array size later
- useful when wanting to set the array size that you know it never changes, eg. months_list
-
- vector types = infinite scalable, better than array.
3.3 function
- can be any where in file.
- must always determine parameter type. fn test(a:i32){ ... }